<?php
namespace Home\Controller;
use Think\Controller;
use Home\Biz\CommonBiz;


class IndexController extends Controller {
    
	public function test1(){
		$Data = 6545646; 
		F('hh', $Data, TEMP_PATH);
	}
	
	public function test(){
	    echo '11';
		$this->display('index/test');
	}
	
	
	public function index(){
		//验证当前用户是否登陆
    	$user_id = cookie('login_user_id');
    	if(empty($user_id)){
    		//未登录
    		redirect(C('UrlConfig.home_page'));
    		return;
    	}
    	else{
    		redirect(C('UrlConfig.home_page'));
    	}
    }
    
    
    
    public function alipaybatchNotify(){
    	//建立请求
    	import('Vendor.alipay.alipay_notify');
    	
    	$alipay_config = array();
    	$alipay_config = C('AlipayBatchConfig');
    	
    	//计算得出通知验证结果
		$alipayNotify = new \AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyNotify();
    	
		if($verify_result) {
			//验证成功
		
			/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//请在这里加上商户的业务逻辑程序代
			
			
			//——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
			
			//获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
			
			$params_text	= CommonBiz::getParamsStr();
			
			
			//批量付款数据中转账成功的详细信息
			$success_details 		= $_POST['success_details'];
			$lst_success_withdraw_str	= explode('|', $success_details);
			foreach ($lst_success_withdraw_str as $success_withdraw_str){
				$withdraw_str_array		= explode('^', $success_withdraw_str);
				$promoter_withdraw_id	= $withdraw_str_array[0];
				$alipay_trade_no		= $withdraw_str_array[6];
				
				//记录所有支付log到在线支付log表
				$onlinePaymentLog = array(
						'order_id'	=> $promoter_withdraw_id,
						'trade_no'	=> $alipay_trade_no,
						'trade_status'	=> 'SUCCESS',
						'payment_method'=> 50,
						'payment_status'=> 1,
						'params_text'	=> $params_text,
						'create_user'	=> 'whaleyFZ.admin.Index alipaybatchNotify',
						'create_date'	=> date("y-m-d H:i:s", time()),
				);
				M('onlinePaymentLog', '', 'DB_trade')->add($onlinePaymentLog);
				
				//更新提现表的提现状态
				$update_promoter_withdraw_en = array(
						'status'		=> 2,
						'success_date'	=> date("y-m-d H:i:s", time()),
				);
				M('promoterWithdraw')->where("id=$promoter_withdraw_id")->save($update_promoter_withdraw_en);
			}
			
			
			//批量付款数据中转账失败的详细信息
			$fail_details = $_POST['fail_details'];
			$lst_fail_withdraw_str	= explode('|', $fail_details);
			foreach ($lst_fail_withdraw_str as $fail_withdraw_str){
				$withdraw_str_array		= explode('^', $fail_withdraw_str);
				$promoter_withdraw_id	= $withdraw_str_array[0];
				$alipay_trade_no		= $withdraw_str_array[6];
			
				//记录所有支付log到在线支付log表
				$onlinePaymentLog = array(
						'order_id'	=> $promoter_withdraw_id,
						'trade_no'	=> $alipay_trade_no,
						'trade_status'	=> 'FAIL',
						'payment_method'=> 50,
						'payment_status'=> 0,
						'params_text'	=> $params_text,
						'create_user'	=> 'whaleyFZ.admin.Index alipaybatchNotify',
						'create_date'	=> date("y-m-d H:i:s", time()),
				);
				M('onlinePaymentLog', '', 'DB_trade')->add($onlinePaymentLog);
				
				//更新提现表的提现状态
				$update_promoter_withdraw_en = array(
						'status'		=> 3,
				);
				M('promoterWithdraw')->where("id=$promoter_withdraw_id")->save($update_promoter_withdraw_en);
			}
			
			
			//判断是否在商户网站中已经做过了这次通知返回的处理
			//如果没有做过处理，那么执行商户的业务程序
			//如果有做过处理，那么不执行商户的业务程序
			
			echo "success";		//请不要修改或删除
			
			//调试用，写文本函数记录程序运行情况是否正常
			//logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
			
			//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
			
			/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		}
    	else{
    		//验证失败
    		echo "fail";
    		
    		//调试用，写文本函数记录程序运行情况是否正常
    		//logResult("这里写入想要调试的代码变量值，或其他运行的结果记录");
    	}
    	
    	
    	
    }
    
}